package com.example.mydy.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.example.mydy.fragment.AvailableRewardsFragment;
import com.example.mydy.fragment.MyRewardsFragment;

/**
 * 悬赏任务页面的ViewPager2适配器，管理两个标签页
 */
public class RewardPagerAdapter extends FragmentStateAdapter {
    
    private AvailableRewardsFragment availableRewardsFragment;
    private MyRewardsFragment myRewardsFragment;

    public RewardPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
        
        // 创建Fragment实例
        availableRewardsFragment = AvailableRewardsFragment.newInstance();
        myRewardsFragment = MyRewardsFragment.newInstance();
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        // 根据位置返回相应的Fragment
        switch (position) {
            case 0:
                return availableRewardsFragment;
            case 1:
                return myRewardsFragment;
            default:
                return availableRewardsFragment;
        }
    }

    @Override
    public int getItemCount() {
        // 总共有两个标签页
        return 2;
    }
    
    /**
     * 刷新所有Fragment的数据
     */
    public void refreshData() {
        if (availableRewardsFragment != null) {
            availableRewardsFragment.refreshData();
        }
        
        if (myRewardsFragment != null) {
            myRewardsFragment.refreshData();
        }
    }
} 